package com.kamistoat.meimeimall.meimeimallcoupon.dubbo;

import com.kamistoat.meimeimall.common.To.couponTo.CouponUpdateTo;
import com.kamistoat.meimeimall.common.To.productTo.SkuReductionTo;
import com.kamistoat.meimeimall.common.To.productTo.SpuBoundsTo;
import com.kamistoat.meimeimall.common.To.seckillTo.SecKillSessionTo;
import com.kamistoat.meimeimall.common.annotation.DubboAndHTTPExceptionCatch;
import com.kamistoat.meimeimall.common.dubboInterface.CouponDubboInterface;
import com.kamistoat.meimeimall.common.utils.R;
import com.kamistoat.meimeimall.meimeimallcoupon.dao.SpuBoundsDao;
import com.kamistoat.meimeimall.meimeimallcoupon.entity.SpuBoundsEntity;
import com.kamistoat.meimeimall.meimeimallcoupon.service.SkuFullReductionService;
import com.kamistoat.meimeimall.meimeimallcoupon.service.SpuBoundsService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * Dubbo接口实现类
 */
@Service
@Component
public class CouponDubboInterfaceImpl implements CouponDubboInterface {
    @Autowired
    SpuBoundsDao spuBoundsDao;
    @Autowired
    SpuBoundsService spuBoundsService;

    @Autowired
    SkuFullReductionService skuFullReductionService;

    /**
     * 获取商品的积分信息
     *
     * @param spuId spuId
     * @return R对象 内含 "bounds" - BigDecimal[]
     */
    @Override
    @DubboAndHTTPExceptionCatch
    public R getBoundsBySpuId(Long spuId) {
        SpuBoundsEntity boundsEntity = spuBoundsService.getBoundBySpuId(spuId);
        BigDecimal[] bounds = new BigDecimal[2];
        if (boundsEntity != null) {
            bounds[0] = boundsEntity.getGrowBounds();
            bounds[1] = boundsEntity.getBuyBounds();
        }
        return R.ok().put("bounds", bounds);
    }

    /**
     * 添加新Spu时，保存Spu的积分设置
     *
     * @param spuBoundsTo Spu积分设置
     * @return R
     */
    @Override
    @DubboAndHTTPExceptionCatch
    public R saveSpuBounds(SpuBoundsTo spuBoundsTo) {
        SpuBoundsEntity spuBoundsEntity = new SpuBoundsEntity();
        BeanUtils.copyProperties(spuBoundsTo,spuBoundsEntity);
        spuBoundsDao.insert(spuBoundsEntity);
        return R.ok();
    }

    /**
     * 添加新Sku时，保存Sku的满减设置
     *
     * @param skuReductionTo Sku满减设置
     * @return R
     */
    @Override
    @DubboAndHTTPExceptionCatch
    public R saveSkuReduction(SkuReductionTo skuReductionTo) {
        skuFullReductionService.saveSkuReductionTo(skuReductionTo);
        return R.ok();
    }

    @Override
    public R updateOrderCoupon(CouponUpdateTo couponUpdateTo) {
        return null;
    }
}
